<?php
/**
 * 易宝支付渠道
 */
require_once (S_ROOT.'./source/action/paymentBase.php');

class act_yeepay_payment extends paymentBase
{
	const PAYURL = "http://tech.yeepay.com:8080/robot/debug.action";//http://www.yeepay.com/app-merchant-proxy/wap/controller.action";
	const KEY = "69cl522AV6q613Ii4W6u8K6XuW8vM1N6bFgyv769220IuYe9u37N4y7rI4Pl";//03vm8y1X4l9J353RWSg0N72Krw81Z87T51003jLW3500g97591662A6W6lQ2";
	const MERID = "10001126856";//10001066165";
	const FLAG = "yeepay";
	private $reqarr = array('p0_Cmd'=>"Buy",'p1_MerId'=>self::MERID,'p2_Order'=>'',
				'p3_Amt'=>'','p4_Cur'=>'CNY','p5_Pid'=>'','p6_Pcat'=>'','p7_Pdesc'=>'',
				'p8_Url'=>'','p9_SAF'=>'0','pa_MP'=>'','pd_FrpId'=>'','pr_NeedResponse'=>'1','hmac'=>'');

	private  $resparr = array('p1_MerId'=>self::MERID,'r0_Cmd'=>'','r1_Code'=>'','r2_TrxId'=>'','r3_Amt'=>'','r4_Cur'=>'','r5_Pid'=>'','r6_Order'=>'','r7_Uid'=>'','r8_MP'=>'','r9_BType'=>'',
			'rb_BankId'=>'','ro_BankOrderId'=>'','rp_PayDate'=>'','rq_CardNo'=>'','ru_Trxtime'=>'','hmac'=>'');
	private $userid;
	
	function __construct($uid) {
		$this->userid = $uid;
		parent::__construct(self::FLAG);
	}
	
	/**
	 * 支付请求，实现抽象类接口
	 */
	public function request_payment()
	{
		global $_SGLOBAL;
//		$client = new HttpClient(self::$payurl);
//		$hmac = get_reqhmacstring();
//		set_reqparam(array('hmac',$hmac));
//		$client->post('',$this->reqarr);
		$hmac = $this->get_reqhmacstring();
		$this->set_params(array('hmac'=>$hmac));
		$this->add_paylog(array('uid'=>$this->userid,'type'=>'req','payid'=>$this->payid,
					'orderid'=>intval($this->reqarr['p2_Order']),'note'=>json_encode($this->reqarr),
					'dateline'=>$_SGLOBAL['timestamp']));
		return $this->reqarr;
	}
	
	/**
	 * 处理支付相应，实现抽象类接口
	 */
	public function parse_payresult()
	{
		global $_SGLOBAL;
		$this->set_orderstatus($this->resparr['r6_Order'],array('status'=>'suc','successtime'=>$_SGLOBAL['timestamp'],'amount'=>$this->resparr['r3_Amt']));
		$this->add_paylog(array('uid'=>$this->userid,'type'=>'resp','payid'=>$this->payid,
					'orderid'=>intval($this->resparr['r6_Order']),'note'=>json_encode($this->resparr),
					'dateline'=>$_SGLOBAL['timestamp']));
		if($this->userid) $this->add_points($this->userid,$this->resparr['r3_Amt']);
	}
	
	/**
	 * 设置请求或相应参数
	 * @param $setarr
	 * @param $type req:请求；resp：响应
	 */
	public function set_params($setarr,$type='req')
	{
		if($type=='req')
		{
			foreach ($setarr as $key => $val)
			{
				$this->reqarr[$key] = $val;
			}
		}
		if($type=='resp')
		{
			foreach ($setarr as $key => $val)
			{
				$this->resparr[$key] = $val;
			}
		}
	}
	
	/**
	 * 生成请求hmac串
	 */
	private function get_reqhmacstring()
	{
		$ret = '';
		foreach($this->reqarr as $key => $val)
		{
			if($key!='hmac')
			{
				$ret .= $val;
			}
		}
		//echo $ret;
		return hash_hmac('md5',$ret,self::KEY);
	}
	/**
	 * callback中验证hmac是否有效
	 */
	public function verify_callbackhmac()
	{
		#取得加密前的字符串
		$sbOld = "";
		#加入商家ID
		$sbOld = $sbOld.$this->resparr['p1_MerId'];
		#加入消息类型
		$sbOld = $sbOld.$this->resparr['r0_Cmd'];
		#加入业务返回码
		$sbOld = $sbOld.$this->resparr['r1_Code'];
		#加入交易ID
		$sbOld = $sbOld.$this->resparr['r2_TrxId'];
		#加入交易金额
		$sbOld = $sbOld.$this->resparr['r3_Amt'];
		#加入货币单位
		$sbOld = $sbOld.$this->resparr['r4_Cur'];
		#加入产品Id
		$sbOld = $sbOld.$this->resparr['r5_Pid'];
		#加入订单ID
		$sbOld = $sbOld.$this->resparr['r6_Order'];
		#加入用户ID
		$sbOld = $sbOld.$this->resparr['r7_Uid'];
		#加入商家扩展信息
		$sbOld = $sbOld.$this->resparr['r8_MP'];
		#加入交易结果返回类型
		$sbOld = $sbOld.$this->resparr['r9_BType'];
		if($this->resparr['hmac']==hash_hmac('md5',$sbOld,self::KEY))
			return true;
		else
			return false;
	}
	
	/**
	 * 创建充值订单
	 * @param unknown_type $uid
	 * @param unknown_type $name
	 */
	public function create_userorder()
	{
		if($this->userid)
			return $this->db_payment->create_order($this->userid);
		else
			return '';
	}
	
	/**
	 * 设置订单状态
	 * @param $orderid
	 * @param $setarr
	 */
	private function set_orderstatus($orderid,$setarr)
	{
		return $this->db_payment->updatetable('payment_order',$setarr,array('id'=>intval($orderid)));
	}
	
}

?>